package org.tio.sitexxx.service.service.qj;

import com.alibaba.fastjson.JSON;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.sitexxx.service.model.main.Charge;
import org.tio.sitexxx.service.vo.Const;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class ChargeService {//用户充值表
    private static Logger log	= LoggerFactory.getLogger(ChargeService.class);
    public static final ChargeService me	= new ChargeService();
    final Charge dao	= new Charge().dao();
    /**
     * 保存数据
     * @param charge
     * @return
     */
    public boolean save(Charge charge) {
        return charge.save();
    }
    /**
     * get all  得到充值套餐
     * return list<Charge>
     */
    public List<Charge>  getAll(){
        List<Charge>list=new ArrayList<>(8);
        String sqlid1 = "charge.getAll";
        String db1 = Const.Db.TIO_SITE_MAIN;
        SqlPara sqlPara1 = Db.use(db1).getSqlPara(sqlid1);
        List<Record> record = Db.use(db1).find(sqlPara1.getSql());
        for(Record record1:record){
            list.add(maptobeen(record1.getColumns()));
        }
        return list;
    }

    /**
     *
     * @param charge_id
     * @return
     */
    public Charge getCharge(int charge_id){
        String sqlid = "charge.getCharge";
        String db = Const.Db.TIO_SITE_MAIN;
        Kv cond=Kv.by("id",charge_id);
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid,cond);
        Record record = Db.use(db).findFirst(sqlPara);
        if(null!=record){
            System.out.println(JSON.toJSONString(record));
            Charge charge=JSON.parseObject(JSON.toJSONString(record.getColumns()),Charge.class);
            return charge;
        }
        return null;
    }
    /**
     * delCharge
     */
    public void delCharge(int id){
        String sqlid = "charge.delCharge";
        String db = Const.Db.TIO_SITE_MAIN;

        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid,id);
        System.out.println("sql:"+sqlPara.getSql()+"all:"+JSON.toJSONString(sqlPara));
        int result = Db.use(db).delete(sqlPara.getSql(),id);
    }
    public static Charge maptobeen(Map<String,Object>map){
        Charge charge=new Charge();
        charge.setReward((Integer)map.get("reward"));
        charge.setCreatedAt((Date)map.get("createdAt"));
        charge.setThreshold((Integer)map.get("threshold"));
        charge.setId((Integer)map.get("id"));
        charge.setUpdatedAt((Date)map.get("updatedAt"));
        return charge;
    }
}
